<?php

/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file template.class.php
 * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
 * @date 2010-12-13
 * @description This object can parse an HTML/Ajax template, check the documentation to find out the detailed parsing rules.
 */
 
if (!defined('IN_IMBA')) exit('Access Denied.');

class Template
{
	protected $fileName;
	private $type;
	protected $style;
    
    /**
	 * @name __construct
	 * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
     * @parameters
     * 	string $filename: The name of an HTML/Ajax template file.
     *  string $type: The type to of the template(HTML or Ajax). 
     *  string $style: The style of the parsed file.(If you input nothing, it will use the common style or the style of its parent.)
	 **/
	public function __construct($fileName, $style = 'common', $type = 'htm')
	{
		$this->fileName = $fileName;
		$this->style = $style;
		$this->type = $type;
	}

    /**
	 * @name __destruct
	 * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
	 **/
	public function __destruct()
	{
	}
    
    /**
     * @name GetPath
     * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
     * @date 2010-12-12
     * @description parse the HTML template and return the path of the parsed file.
     * @return string: The path of the template file which is prased.
     */
	public function GetPath()
	{
		return $this->template();
	}

    /**
     * @name tplrefresh
     * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
     * @date 2010-12-12
     * @description Find out whether a template need to be refreshed.
     * @parameters
     * 	string $fileobj: The name of file which has been prased.
     *  string $templatename: The name of an HTML/Ajax template file.
     * @return
     * 	bool: Return true if the template file need to be refreshed, else return false.
    */
	protected function tplrefresh($fileobj, $templatename)
	{
		if (!file_exists($fileobj) || !is_readable($fileobj)) return true;
		if (filemtime($fileobj) > @filemtime($templatename)) return false;
		else  return true;
	}

    /**
     * @name IsLegalVar
     * @deprecated Do not use this function, this function is deprecated and maybe removed in later versions.
     * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
     * @date 2010-12-12
     * @description Find out whether a name of var is legal.
     * @parameters
     * 	string $var: The name of var to judge.
     * @return
     * 	bool: Return the prased words if the name of the var is legal, else return ''.
    */
	private function IsLegalVar($var)
	{
		$var = preg_replace('/\\$(.+?)(?=\\[|->|\\b)(.*)/', '$GLOBALS[\'$1\']$2', $var);
		eval('$i = ' . $var . ';');
		if (isset($i)) return '<?php echo ' . $var . ';?>';
		else  return '';
	}
    
    /**
     * @name template
     * @author deltamaster, Night Kids, Little Zhi, charleychaoyi, ST, wrongway
     * @date 2010-12-12
     * @description Parse an HTML/Ajax template, check the documentation to find out the detailed parsing rules.
     * @return
     * 	string: Return path of the prased file.
    */
	private function template()
	{
		if (!file_exists(IMBA_CACHE_DIR . 'template/common/') && @mkdir(IMBA_CACHE_DIR . 'template/common/', 0777, true));
		if (!file_exists(IMBA_CACHE_DIR . 'template/' . $this->style . '/') && @mkdir(IMBA_CACHE_DIR . 'template/' . $this->style . '/', 0777, true));
		
		$templatename = IMBA_TEMPLATE_DIR . $this->style . '/' . $this->fileName . '.' . $this->type;
		$fileobj = IMBA_CACHE_DIR . 'template/' . $this->style . '/' . $this->fileName . '.tpl.php';
		if (!file_exists($templatename))
		{
			$templatename = IMBA_TEMPLATE_DIR . 'common/' . $this->fileName . '.' . $this->type;
			$fileobj = IMBA_CACHE_DIR . 'template/common/' . $this->fileName . '.tpl.php';
		}

		if (!$this->tplrefresh($fileobj, $templatename)) return $fileobj;
		if (strtolower($this->type) == 'htm')
		{
			if (file_exists($templatename))
			{

				$temp = "<?php if (!defined('IN_IMBA')) exit('Access Denied.'); ?>\n";
                $temp .= file_get_contents($templatename);
                $temp = preg_replace("/\{\s*form\s+([a-zA-Z0-9]+?)\s+([a-zA-Z0-9]+?)\s*\}/", "<?php \$form[\"$1\"] = new Form(\"$1\", \"$2\"); include(\$form[\"$1\"]->GetPath());?>",$temp);
				$temp = preg_replace("/\{\s*template\s*}/", "<?php \$form[\"$1\"] = new Form(\"$1\", \"$this->style\"); include(\$form[\"$1\"]->GetPath()); ?>",$temp);
                $temp = preg_replace("/\{\s*template\s+([a-zA-Z0-9]+?)\s+([a-zA-Z0-9]+?)\s*\}/", "<?php \$template[\"$1\"] = new Template(\"$1\", \"$2\"); include(\$template[\"$1\"]->GetPath());?>",
					$temp);
				$temp = preg_replace("/\{\s*template\s+([a-zA-Z0-9]+?)\s*}/", "<?php \$template[\"$1\"] = new Template(\"$1\", \"$this->style\"); include(\$template[\"$1\"]->GetPath()); ?>",
					$temp);
				$temp = preg_replace("/<!--\s*{if\s+(.+?)}\s*-->/", "<?php if ($1) { ?>", $temp);
				$temp = preg_replace("/<!--\s*{else}\s*-->/", "<?php } else { ?>", $temp);
				$temp = preg_replace("/<!--\s*{else\s*if\s+(.+?)}\s*-->/", "<?php } else if ($1) { ?>", $temp);
				$temp = preg_replace("/<!--\s*{\/if}\s*-->/", "<?php } ?>", $temp);
				$temp = preg_replace("/<!--\s*{loop\s+\\$(.+?)\s+\\$(.+?)\s+\\$(.+?)}\s*-->/", "<?php foreach ($$1 as $$2 => $$3) { ?>", $temp);
				$temp = preg_replace("/<!--\s*{loop\s+\\$(.+?)\s+\\$(.+?)}\s*-->/", "<?php foreach ($$1 as $$2) { ?>", $temp);
				$temp = preg_replace("/<!--\s*{\/loop}\s*-->/", "<?php } ?>", $temp);
				/*
				while (preg_match("/\{\s*((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)\s*\}/", $temp, $match) != 0)
				{
					$result = $this->IsLegalVar($match[1]);
					$temp = str_replace($match[0], $result, $temp);
					$match = array();
				}*/
				$temp = preg_replace("/\{\s*((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)\s*\}/", "<?php echo $1;?>", $temp);
				
				$temp = preg_replace("/\{\s*lang\s+(\w+?)\s*\}/", "<?php echo \$lang['\$1']; ?>", $temp);
				$temp = preg_replace("/\{\s*lang\s+(\w+?)\s+(\w+?)\s*\}/", "<?php echo \$lang['\$1']['\$2']; ?>", $temp);
				$temp = trim($temp);
				file_put_contents($fileobj, $temp);
				return $fileobj;
			}
			else
			{
				die("Template file cannot be found.");
			}
		}
	}

}

?>